import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2023-10-07
 * Time: 14:17
 */

//螺旋矩阵
public class Spiralarray_1 {
    public static int[][] generateMatrix(int n){
        int[][] res = new int[n][n];
        if(n==0){
            return res;
        }
        int left = 0;
        int right = n-1;
        int up = 0;
        int down = n-1;
        int i = 1;
        while(i<=n*n){
            for(int col = left;col<=right;col++){
                res[up][col] = i;
                i++;
            }
            up++;
            if(i<=n*n){
                for(int j =up;j<=down;j++){
                    res[j][right] = i;
                    i++;
                }
                right--;
            }
            if(i<=n*n){
                for(int j = right;j>=left;j--){
                    res[down][j]=i;
                    i++;
                }
                down--;
            }
            if(i<=n*n){
                for(int j = down;j>=up;j--){
                    res[j][left]=i;
                    i++;
                }
                left++;
            }
        }
        return res;
    }
    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printMatrix(generateMatrix(6));
    }
}
